﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 盘点
    /// </summary>
    public class CycleCountService : BaseService<BaseEntity>, ICycleCountService
    {
        private readonly ICycleCountRepository repository;
        public CycleCountService(ICycleCountRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        //获取所有盘点状态下的单据
        public SetObjectDetail GetCycleCountOrderList(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<WmsCheckStock> wmsChecks = repository.GetCycleCountOrderList(orderNo);
            if (wmsChecks.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsChecks.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        //盘点单根据单号获取盘点单明细
        public SetObjectDetail GetCycleCountOrderListByOrderNo(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<WmsCheckStock> wmsChecks = repository.GetCycleCountOrderListByOrderNo(orderNo);
            if (wmsChecks.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsChecks.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        //输入库位，验证
        public SetObjectDetail GetLocateByName(string locateName, string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);
            if (string.IsNullOrEmpty(orderNo))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:单据不能为空，请选择单据!";
                return rfInfo;
            }

            if (sysLocates == null)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
                return rfInfo;
            }
            else if (sysLocates.Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
                return rfInfo;
            }

            WmsCheckStockRange wmsChecks = repository.GetWmsCheckStockRangeByLocateName(orderNo, locateName, sysLocates.ZoneName);
            if (wmsChecks == null)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不在盘点单范围内";
                return rfInfo;
            }

            List<WmsCheckStock_Extend> checkStocks = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);

            rfInfo.type = "PASS";
            rfInfo.message = "库位扫描成功，请扫描箱条码！";
            rfInfo.wmsObjectDetail = checkStocks.ConvertAll(s => (object)s);

            return rfInfo;
        }


        /// <summary>
        /// /批量处理箱条码
        /// </summary>
        /// <param name="orderNo">单号</param>
        /// <param name="locateName"></param>
        /// <param name="cartonNo">周转箱条码</param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckStockByCartonListAlocate(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            //order = order.Substring(0, order.Length - 1);
            //List<string> cartonNo = order.Split(",").ToList();
            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (cartonNo.Count() == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            ///查询盘点明细表中周转箱的条码数据
            List<WmsCheckStockList_Extend> checkStockListInfos = repository.GetWmsCheckStockListkByBox(orderNo, cartonNo, sysLocates.LocateId.ToString());

            //checkStockListInfos = checkStockListInfos.Where(s => s.packageType == 0).ToList();
            for (int i = 0; i < checkStockListInfos.Count(); i++)
            {
                //List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo[i]);
                

                List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, checkStockListInfos[i].CartonNo);

                int qty = (( checkStockListInfos[i].TransStatus != 10) ? Convert.ToInt32(checkStockListInfos[i].FactQty) : Convert.ToInt32(checkStockListInfos[i].Qty));//实盘的数量
                if (wmsCheckStocks[0].IsObvious == 1)
                {
                    if (wmsCheckStocks[0].OrderStatus == "20")//修改为盘点中状态
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                    if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                    {
                        //提示确认框
                        rfInfo.type = "LOCPRO";
                        rfInfo.message = "库位不一致，请确认是否继续!";
                        return rfInfo;
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) == qty)//一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='20'," +
                                      " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                      "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                      "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                      ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + checkStockListInfos[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) < qty)//盘盈
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='30'," +
                                 " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                 "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                 "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                 ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + checkStockListInfos[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) > qty)//盘亏
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='40'," +
                                     " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                     "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                     "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                     ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + checkStockListInfos[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                    
                }
                else
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                }

            }

            int refNum = repository.SQLTransaction(sqlStrings, parameterList);
            if (refNum > 0)
            {
                List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                rfInfo.type = "OK";
                rfInfo.message = "操作成功!请继续扫描箱条码";
                rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }


            return rfInfo;
        }



        // 扫描箱条码
        public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

            List<WmsCheckStockRange> wmsChecks = new List<WmsCheckStockRange>();

            #region 亿日中的扫描周转箱条码逻辑
            if (sysStocks[0].CartonType == 1)
            {

                //List<SysStock> sysRackPackageStocks = repository.GetRackPackageStockByCaronNo(cartonNo);
                //if (sysRackPackageStocks.Count == 0)
                //{
                //    //提示  [M0090105]:该条码不可用，请检查!
                //    rfInfo.type = "FAIL";
                //    rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                //    return rfInfo;
                //}

                //List<WmsCheckStock> wmsChecksStockQty =new List<WmsCheckStock>();

                //foreach (var item in sysRackPackageStocks)
                //{
                //    wmsChecks = repository.GetWmsCheckStockRangeByIsExsistPartNo(orderNo, sysLocates.LocateName, sysLocates.ZoneName, item.PartNo);
                //    if (wmsChecks.Count > 0)
                //    {
                //        rfInfo.type = "FAIL";
                //        rfInfo.message = "[" + cartonNo + "]:该周转箱条码中有箱条码不在盘点单范围内，请检查！";
                //        return rfInfo;
                //    }

                //    List<WmsCheckStock> infos = repository.GetWmsCheckStockQtyFactByTurnoverBoxLocateName(orderNo, locateName, item.CartonNo);
                //    if (infos.Count > 0)
                //    {
                //        wmsChecksStockQty.Add(infos[0]);
                //    }

                //}
                //List<WmsCheckStock> wmsCheckStocksYr = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);//存在周转箱的时候
                //if (wmsCheckStocksYr.Count == 0)
                //{
                //    List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                //    if (checkStocks[0].IsObvious == 1)
                //    {
                //        //提示确认框
                //        rfInfo.type = "ADDPRO";
                //        rfInfo.message = "新增盘点条码，请确认是否继续!";
                //        return rfInfo;
                //    }
                //    else
                //    {
                //        rfInfo.type = "PASS";
                //        rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                //        return rfInfo;
                //    }

                //}
                ///查询盘点明细表中周转箱的条码数据
                List<WmsCheckStockList_Extend> checkStockListInfos = repository.GetWmsCheckStockListkByBox(orderNo, cartonNo, sysLocates.LocateId.ToString());
                if (checkStockListInfos.Count == 0)
                {
                    //提示  [M0090105]:该条码不可用，请检查!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                    return rfInfo;
                }
                foreach (var item in checkStockListInfos)
                {
                    wmsChecks = repository.GetWmsCheckStockRangeByIsExsistPartNo(orderNo, sysLocates.LocateName, sysLocates.ZoneName, item.PartNo);
                    if (wmsChecks.Count > 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + cartonNo + "]:该周转箱条码中有箱条码不在盘点单范围内，请检查！";
                        return rfInfo;
                    }
                }
                List<WmsCheckStock> wmsCheckStocksYr = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);//存在周转箱的时候
                if (wmsCheckStocksYr.Count == 0)
                {
                    List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                    if (checkStocks[0].IsObvious == 1)
                    {
                        //提示确认框
                        rfInfo.type = "ADDPRO";
                        rfInfo.message = "新增盘点条码，请确认是否继续!";
                        return rfInfo;
                    }
                    else
                    {
                        rfInfo.type = "PASS";
                        rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                        return rfInfo;
                    }

                }

                if (checkStockListInfos.Count > 0)
                {
                    
                    checkStockListInfos = checkStockListInfos.Where(x => x.packageType == 0).ToList();
                    rfInfo.type = "TURNOVERBOX";//周装箱
                    rfInfo.message = "扫描成功，请操作下面条箱条码";
                    rfInfo.wmsObjectDetail = checkStockListInfos.ConvertAll(s => (object)s);
                    return rfInfo;
                }

            }


            #endregion

            wmsChecks = repository.GetWmsCheckStockRangeByIsExsistPartNo(orderNo, sysLocates.LocateName, sysLocates.ZoneName, sysStocks[0].PartNo);
            if (wmsChecks.Count > 0)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不在盘点单范围内，请检查！";
                return rfInfo;
            }

            List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);
            if (wmsCheckStocks.Count == 0)
            {
                List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                if (checkStocks[0].IsObvious == 1)
                {
                    //提示确认框
                    rfInfo.type = "ADDPRO";
                    rfInfo.message = "新增盘点条码，请确认是否继续!";
                    return rfInfo;
                }
                else
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                    return rfInfo;
                }

            }
            if (wmsCheckStocks[0].OrderType == "20")//静态盘点
            {
                if (sysStocks[0].Enabled != "N")
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + orderNo + "]:该单据静态盘点未冻结条码，请检查！";
                    return rfInfo;
                }
            }
            else if (wmsCheckStocks[0].OrderStatus != "20" && wmsCheckStocks[0].OrderStatus != "30")//开始盘点 盘点中
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:该单据未开启盘点，请检查！";
                return rfInfo;
            }
            else if (wmsCheckStocks[0].TransStatus!=10|| wmsCheckStocks[0].FactQty != "0")//已盘点
            {
                //提示确认框
                rfInfo.type = "REDO";
                rfInfo.message = "[" + cartonNo + "]:重复扫描箱条码，是否确认数量？";
                return rfInfo;
            }


            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            if (wmsCheckStocks[0].IsObvious == 1)
            {
                if (wmsCheckStocks[0].OrderStatus == "20")//修改为盘点中状态
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }

                if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                {
                    //提示确认框
                    rfInfo.type = "LOCPRO";
                    rfInfo.message = "库位不一致，请确认是否继续!";
                    return rfInfo;
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) == Convert.ToInt32(sysStocks[0].Qty))//一致
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + sysStocks[0].Qty + ",trans_status='20'," +
                                  " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                  "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                  "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                  ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) < Convert.ToInt32(sysStocks[0].Qty))//盘盈
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + sysStocks[0].Qty + ",trans_status='30'," +
                             " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                             "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                             "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                             ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) > Convert.ToInt32(sysStocks[0].Qty))//盘亏
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + sysStocks[0].Qty + ",trans_status='40'," +
                                 " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                 "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                 "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                 ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }


                int refNum = repository.SQLTransaction(sqlStrings, parameterList);
                if (refNum > 0)
                {
                    List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);

                    foreach (var item in wmsChecks1)
                    {
                        if (item.PartNo == sysStocks[0].PartNo)
                        {
                            item.isColor = 1;
                            break;
                        }
                        
                    }

                    rfInfo.type = "PASSOBV";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                }
                else
                {
                    //提示  操作失败!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
            }
            return rfInfo;
        }


        /// <summary>
        /// 点击周转箱条码中的箱条码
        /// </summary>
        /// <param name="turnoverBoxNo">周转箱条码</param>
        /// <param name="cartonNo">扫码或选中的条码</param>
        /// <param name="locateName">库位</param>
        /// <param name="orderNo">单号</param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckStockByCartonBox(string turnoverBoxNo,string cartonNo, string locateName, string orderNo,  string loginId,string qty)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            if (string.IsNullOrEmpty(orderNo))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:单据不能为空，请选择单据!";
                return rfInfo;
            }

           
            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

            //查询盘点明细表中周转箱的条码数据
            List<WmsCheckStockList_Extend> checkStockListInfos = repository.GetWmsCheckStockListkByBox(orderNo, turnoverBoxNo, sysLocates.LocateId.ToString());

            WmsCheckStockList_Extend wmsCheckStockListInfo =new WmsCheckStockList_Extend();

            wmsCheckStockListInfo = checkStockListInfos.Where(s=>s.CartonNo== cartonNo).FirstOrDefault();//只有一条修改数据

            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();


            #region  修改周转箱的数据
            WmsCheckStockList_Extend _wmsCheckStockListInfo = new WmsCheckStockList_Extend();

            _wmsCheckStockListInfo = checkStockListInfos.Where(s => s.packageType == 1).FirstOrDefault();//只有一条修改数据
            if (_wmsCheckStockListInfo != null)
            {
                stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + 1 + ",trans_status='20'," +
                    " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                    "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                    "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                    ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + _wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "' ;");
                sqlStrings.Add(stringBuilder.ToString());
            }

            #endregion



            if (wmsCheckStockListInfo.LocateName.ToUpper() != locateName.ToUpper())//库位不一致
            {
                stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='60'," +
                    " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                    "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                    "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                    ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "' ;");
                sqlStrings.Add(stringBuilder.ToString());
            }
            else {
                List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, wmsCheckStockListInfo.CartonNo);
                
                if (wmsCheckStocks.Count > 0)//修改数量
                {
                    if (wmsCheckStocks[0].OrderStatus == "20")//修改为盘点中状态
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='60'," +
                            " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                            "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                            "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                            ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "' ;");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) == Convert.ToInt32(qty))//一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='20'," +
                                      " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                      "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                      "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                      ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) < Convert.ToInt32(qty))//盘盈
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='30'," +
                                 " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                 "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                 "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                 ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) > Convert.ToInt32(qty))//盘亏
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='40'," +
                                     " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                     "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                     "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                     ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + wmsCheckStockListInfo.CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                }

            }

            int refNum = repository.SQLTransaction(sqlStrings, parameterList);
            if (refNum>0)
            {
                 checkStockListInfos=new List<WmsCheckStockList_Extend>();
                 checkStockListInfos = repository.GetWmsCheckStockListkByBox(orderNo, turnoverBoxNo, sysLocates.LocateId.ToString());
                checkStockListInfos = checkStockListInfos.Where(s => s.packageType == 0).ToList();
                foreach (var item in checkStockListInfos)
                {
                    if (item.CartonNo == cartonNo)
                    {
                        item.isColor = 1;
                        break;
                    }
                    
                }


                rfInfo.type = "PASS";
                rfInfo.message = "操作成功!请继续选择箱条码";
                rfInfo.wmsObjectDetail = checkStockListInfos.ConvertAll(s => (object)s);
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;

        }

        /// <summary>
        /// 扫描箱条码，库位不一致(亿日)
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="locateName"></param>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckStockFactQtyByLocatePrompt(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            #region 亿日的周转箱
            if (sysStocks[0].CartonType == 1)
            {
                sysStocks = new List<SysStock>();
                sysStocks = repository.GetRackPackageStockByCaronNo(cartonNo);
            }

            #endregion

            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);
            List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);

            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            if (wmsCheckStocks[0].IsObvious == 1)
            {
                for (int i = 0; i < sysStocks.Count(); i++)
                {
                    if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + sysStocks[i].Qty + ",trans_status='60'," +
                            " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                            "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                            "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                            ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "' and order_no='" + orderNo + "' ;");
                        sqlStrings.Add(stringBuilder.ToString());
                    }

                }
               

                int refNum = repository.SQLTransaction(sqlStrings, parameterList);
                if (refNum > 0)
                {
                    List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);

                    rfInfo.type = "PASSOBV";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                }
                else
                {
                    //提示  操作失败!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
            }
            return rfInfo;
        }

        // 扫描箱条码，库位不一致
        //public SetObjectDetail CheckStockFactQtyByLocatePrompt(string cartonNo, string locateName, string orderNo, string loginId)
        //{
        //    SetObjectDetail rfInfo = new SetObjectDetail();
        //    List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
        //    if (sysStocks.Count == 0)
        //    {
        //        //提示  [M0090105]:该条码不可用，请检查!
        //        rfInfo.type = "FAIL";
        //        rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
        //        return rfInfo;
        //    }
        //    SysLocate sysLocates = repository.GetLocateByLocateName(locateName);
        //    List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);

        //    StringBuilder stringBuilder = new StringBuilder(1024);
        //    List<string> sqlStrings = new List<string>();
        //    List<DynamicParameters> parameterList = new List<DynamicParameters>();
        //    if (wmsCheckStocks[0].IsObvious == 1)
        //    {
        //        if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
        //        {
        //            stringBuilder = new StringBuilder(1024);
        //            stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + sysStocks[0].Qty + ",trans_status='60'," +
        //                " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
        //                "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
        //                "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
        //                ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "' ;");
        //            sqlStrings.Add(stringBuilder.ToString());
        //        }

        //        int refNum = repository.SQLTransaction(sqlStrings, parameterList);
        //        if (refNum > 0)
        //        {
        //            List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);

        //            rfInfo.type = "PASSOBV";
        //            rfInfo.message = "操作成功!请继续扫描箱条码";
        //            rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
        //        }
        //        else
        //        {
        //            //提示  操作失败!
        //            rfInfo.type = "FAIL";
        //            rfInfo.message = "未查询到数据，请检查！";
        //        }
        //    }
        //    else
        //    {
        //        rfInfo.type = "PASS";
        //        rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
        //    }
        //    return rfInfo;
        //}

        //扫描箱条码 新增

        /// <summary>
        /// //扫描箱条码 新增（亿日）
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="locateName"></param>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckStockFactQtyByAddPrompt(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            #region 亿日的周转箱
            if (sysStocks[0].CartonType == 1)
            {
                sysStocks = new List<SysStock>();
                sysStocks = repository.GetRackPackageStockByCaronNo(cartonNo);
            }

            #endregion

            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);
            List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);

            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            if (wmsCheckStocks.Count == 0)
            {
                //盘点单不存在此条码，新增
                List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                if (checkStocks[0].IsObvious == 1)
                {
                    List<WmsCheckStockList_Extend> wmsCheckStockLists=new List<WmsCheckStockList_Extend>();
                    for (int i = 0; i < sysStocks.Count(); i++)
                    {
                        WmsCheckStockList_Extend checkStockList = new WmsCheckStockList_Extend();
                        checkStockList.OrderNo = orderNo;
                        checkStockList.WarehouseId = sysStocks[i].WarehouseId; checkStockList.WarehouseName = sysStocks[i].WarehouseName;
                        checkStockList.ZoneId = sysStocks[i].ZoneId; checkStockList.ZoneName = sysStocks[i].ZoneName;
                        checkStockList.LocateId = sysStocks[i].LocateId; checkStockList.LocateName = sysStocks[i].LocateName;
                        checkStockList.PartId = sysStocks[i].PartId; checkStockList.PartNo = sysStocks[i].PartNo;
                        checkStockList.PartSpec = sysStocks[i].PartSpec; checkStockList.CartonNo = sysStocks[i].CartonNo;
                        checkStockList.Qty = 0; checkStockList.FactQty = Convert.ToDecimal(sysStocks[i].Qty);

                        checkStockList.DestWarehouseId = sysLocates.WarehouseId; checkStockList.DestWarehouseName = sysLocates.WarehouseName;
                        checkStockList.DestZoneId = sysLocates.ZoneId; checkStockList.DestZoneName = sysLocates.ZoneName;
                        checkStockList.DestLocateId = sysLocates.LocateId; checkStockList.DestLocateName = sysLocates.LocateName;

                        checkStockList.TransStatus = 50;//新增
                        checkStockList.Unit = sysStocks[i].Unit; checkStockList.SnStatus = "60";
                        checkStockList.FactoryId = sysStocks[i].FactoryId; checkStockList.FactoryCode = sysStocks[i].FactoryCode;
                        checkStockList.Enabled = "Y"; checkStockList.CreateUserId = Convert.ToInt32(loginId);
                        checkStockList.packageType = sysStocks[i].CartonType;

                        List<WmsRackPackage_Extend> wmsRackPackages = new List<WmsRackPackage_Extend>();
                        if (sysStocks[i].CartonType == 0)
                        {
                            wmsRackPackages = repository.GetWmsRackPackageInfo(sysStocks[i].CartonNo, sysStocks[i].FactoryId.ToString());
                        }


                        checkStockList.turnoverBoxNo = wmsRackPackages.Count()>0?wmsRackPackages[0].RackNo: "";

                        wmsCheckStockLists.Add(checkStockList);

                    }

                    //不在盘点单内 添加条码到明细
                    int recCheckStockList = repository.InsertCheckStockListInfos(wmsCheckStockLists);
                    if (recCheckStockList > 0)
                    {
                        List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                        rfInfo.type = "PASSOBV";
                        rfInfo.message = "操作成功!请继续扫描箱条码";
                        rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                        return rfInfo;
                    }
                    else
                    {
                        List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                        rfInfo.type = "FAIL";
                        rfInfo.message = "操作失败!请检查";
                        rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                        return rfInfo;
                    }
                }
                else
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                    return rfInfo;
                }
            }
            else
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
            }
            return rfInfo;

        }

        /*
        //扫描箱条码 新增
        public SetObjectDetail CheckStockFactQtyByAddPrompt(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

          
            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);
            List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);

            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            if (wmsCheckStocks.Count == 0)
            {
                //盘点单不存在此条码，新增
                List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                if (checkStocks[0].IsObvious == 1)
                {
                    WmsCheckStockList checkStockList = new WmsCheckStockList();
                    checkStockList.OrderNo = orderNo;
                    checkStockList.WarehouseId = sysStocks[0].WarehouseId; checkStockList.WarehouseName = sysStocks[0].WarehouseName;
                    checkStockList.ZoneId = sysStocks[0].ZoneId; checkStockList.ZoneName = sysStocks[0].ZoneName;
                    checkStockList.LocateId = sysStocks[0].LocateId; checkStockList.LocateName = sysStocks[0].LocateName;
                    checkStockList.PartId = sysStocks[0].PartId; checkStockList.PartNo = sysStocks[0].PartNo;
                    checkStockList.PartSpec = sysStocks[0].PartSpec; checkStockList.CartonNo = sysStocks[0].CartonNo;
                    checkStockList.Qty = 0; checkStockList.FactQty = Convert.ToDecimal(sysStocks[0].Qty);

                    checkStockList.DestWarehouseId = sysLocates.WarehouseId; checkStockList.DestWarehouseName = sysLocates.WarehouseName;
                    checkStockList.DestZoneId = sysLocates.ZoneId; checkStockList.DestZoneName = sysLocates.ZoneName;
                    checkStockList.DestLocateId = sysLocates.LocateId; checkStockList.DestLocateName = sysLocates.LocateName;

                    checkStockList.TransStatus = 50;//新增
                    checkStockList.Unit = sysStocks[0].Unit; checkStockList.SnStatus = "60";
                    checkStockList.FactoryId = sysStocks[0].FactoryId; checkStockList.FactoryCode = sysStocks[0].FactoryCode;
                    checkStockList.Enabled = "Y"; checkStockList.CreateUserId = Convert.ToInt32(loginId);

                    //不在盘点单内 添加条码到明细
                    int recCheckStockList = repository.InsertCheckStockListInfo(checkStockList);
                    if (recCheckStockList > 0)
                    {
                        List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                        rfInfo.type = "PASSOBV";
                        rfInfo.message = "操作成功!请继续扫描箱条码";
                        rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                        return rfInfo;
                    }
                    else
                    {
                        List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                        rfInfo.type = "FAIL";
                        rfInfo.message = "操作失败!请检查";
                        rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                        return rfInfo;
                    }
                }
                else
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
                    return rfInfo;
                }
            }
            else
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功!请输入数量";
            }
            return rfInfo;

        }


        */

        /// <summary>
        /// 确认输入数量完成盘点(亿日)
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="locateName"></param>
        /// <param name="orderNo"></param>
        /// <param name="qty"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckStockByCartonAFactQty(string cartonNo, string locateName, string orderNo, string qty, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            if (string.IsNullOrEmpty(orderNo))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:单据不能为空，请选择单据!";
                return rfInfo;
            }

            #region 亿日的周转箱
            if (sysStocks[0].CartonType == 1)
            {
                sysStocks = new List<SysStock>();
                sysStocks = repository.GetRackPackageStockByCaronNo(cartonNo);
            }

            #endregion

            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

         
            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            List<WmsCheckStockList_Extend> wmsCheckStockLists = new List<WmsCheckStockList_Extend>();

            int insertSql = 1;//全部不在单据内新增
            int updateSql = 1;//全部在单据内修改
            for (int i = 0; i < sysStocks.Count(); i++)
            {
                List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, sysStocks[i].CartonNo);

                if (wmsCheckStocks.Count == 0)
                {
                    WmsCheckStockList_Extend checkStockList = new WmsCheckStockList_Extend();
                    checkStockList.OrderNo = orderNo;
                    checkStockList.WarehouseId = sysStocks[i].WarehouseId; checkStockList.WarehouseName = sysStocks[i].WarehouseName;
                    checkStockList.ZoneId = sysStocks[i].ZoneId; checkStockList.ZoneName = sysStocks[i].ZoneName;
                    checkStockList.LocateId = sysStocks[i].LocateId; checkStockList.LocateName = sysStocks[i].LocateName;
                    checkStockList.PartId = sysStocks[i].PartId; checkStockList.PartNo = sysStocks[i].PartNo;
                    checkStockList.PartSpec = sysStocks[i].PartSpec; checkStockList.CartonNo = sysStocks[i].CartonNo;
                    checkStockList.Qty = 0; checkStockList.FactQty = Convert.ToDecimal(sysStocks[i].Qty);

                    checkStockList.DestWarehouseId = sysLocates.WarehouseId; checkStockList.DestWarehouseName = sysLocates.WarehouseName;
                    checkStockList.DestZoneId = sysLocates.ZoneId; checkStockList.DestZoneName = sysLocates.ZoneName;
                    checkStockList.DestLocateId = sysLocates.LocateId; checkStockList.DestLocateName = sysLocates.LocateName;

                    checkStockList.TransStatus = 50;//新增
                    checkStockList.Unit = sysStocks[i].Unit; checkStockList.SnStatus = "60";
                    checkStockList.FactoryId = sysStocks[i].FactoryId; checkStockList.FactoryCode = sysStocks[i].FactoryCode;
                    checkStockList.Enabled = "Y"; checkStockList.CreateUserId = Convert.ToInt32(loginId);
                    checkStockList.packageType = sysStocks[i].CartonType;

                    List<WmsRackPackage_Extend> wmsRackPackages = new List<WmsRackPackage_Extend>();
                    if (sysStocks[i].CartonType == 0)
                    {
                        wmsRackPackages = repository.GetWmsRackPackageInfo(sysStocks[i].CartonNo, sysStocks[i].FactoryId.ToString());
                    }


                    checkStockList.turnoverBoxNo = wmsRackPackages.Count() > 0 ? wmsRackPackages[0].RackNo : "";

                    wmsCheckStockLists.Add(checkStockList);
                    insertSql++;

                }
                if (wmsCheckStocks.Count > 0)
                {
                    #region 更新盘点数据
                    if (wmsCheckStocks[0].OrderStatus == "20")//修改为盘点中状态
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='60'," +
                            " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                            "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                            "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                            ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "' and order_no='" + orderNo + "' ;");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) == Convert.ToInt32(qty))//一致
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='20'," +
                                      " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                      "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                      "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                      ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) < Convert.ToInt32(qty))//盘盈
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='30'," +
                                 " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                 "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                 "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                 ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    else if (Convert.ToInt32(wmsCheckStocks[0].Qty) > Convert.ToInt32(qty))//盘亏
                    {
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='40'," +
                                     " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                     "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                     "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                     ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "' and order_no='" + orderNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());
                    }
                    updateSql++;
                    #endregion
                }

            }
            if (sqlStrings.Count > 0)
            {
                #region 提交更新数据
                int refNum = repository.SQLTransaction(sqlStrings, parameterList);
                if (refNum > 0)
                {
                    List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                    foreach (var item in wmsChecks1)
                    {
                        if (item.PartNo == sysStocks[0].PartNo)
                        {
                            item.isColor = 1;
                            break;
                        }
                        
                    }
                    rfInfo.type = "PASS";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                }
                else
                {
                    //提示  操作失败!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";

                }
                #endregion
            }
            if(wmsCheckStockLists.Count>0)
            {
                #region 提交插入数据
                int recCheckStockList = repository.InsertCheckStockListInfos(wmsCheckStockLists);
                if (recCheckStockList > 0)
                {
                    List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                    rfInfo.type = "PASSOBV";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                }
                else
                {
                    List<WmsCheckStock_Extend> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                    rfInfo.type = "FAIL";
                    rfInfo.message = "操作失败!请检查";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                }
                #endregion
            }
            //if (updateSql > insertSql)
            //{
             

            //}
            //else//不在盘点单内 添加条码到明细
            //{
               
            //}

            return rfInfo;

        }

        /*
        //确认输入数量完成盘点
        public SetObjectDetail CheckStockByCartonAFactQty(string cartonNo, string locateName, string orderNo, string qty, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStockByCaronNo(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            if (string.IsNullOrEmpty(orderNo))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:单据不能为空，请选择单据!";
                return rfInfo;
            }

            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

            List<WmsCheckStock> wmsCheckStocks = repository.GetWmsCheckStockByCartonNo(orderNo, cartonNo);
            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();


            if (wmsCheckStocks.Count == 0)
            {
                //盘点单不存在此条码，新增
                List<WmsCheckStock> checkStocks = repository.GetWmsCheckStockByCartonNo(orderNo);
                WmsCheckStockList checkStockList = new WmsCheckStockList();
                checkStockList.OrderNo = orderNo;
                checkStockList.WarehouseId = sysStocks[0].WarehouseId; checkStockList.WarehouseName = sysStocks[0].WarehouseName;
                checkStockList.ZoneId = sysStocks[0].ZoneId; checkStockList.ZoneName = sysStocks[0].ZoneName;
                checkStockList.LocateId = sysStocks[0].LocateId; checkStockList.LocateName = sysStocks[0].LocateName;
                checkStockList.PartId = sysStocks[0].PartId; checkStockList.PartNo = sysStocks[0].PartNo;
                checkStockList.PartSpec = sysStocks[0].PartSpec; checkStockList.CartonNo = sysStocks[0].CartonNo;
                checkStockList.Qty = 0; checkStockList.FactQty = Convert.ToDecimal(qty);

                checkStockList.DestWarehouseId = sysLocates.WarehouseId; checkStockList.DestWarehouseName = sysLocates.WarehouseName;
                checkStockList.DestZoneId = sysLocates.ZoneId; checkStockList.DestZoneName = sysLocates.ZoneName;
                checkStockList.DestLocateId = sysLocates.LocateId; checkStockList.DestLocateName = sysLocates.LocateName;

                checkStockList.TransStatus = 50;//新增
                checkStockList.Unit = sysStocks[0].Unit; checkStockList.SnStatus = "60";
                checkStockList.FactoryId = sysStocks[0].FactoryId; checkStockList.FactoryCode = sysStocks[0].FactoryCode;
                checkStockList.Enabled = "Y"; checkStockList.CreateUserId = Convert.ToInt32(loginId);

                //不在盘点单内 添加条码到明细
                int recCheckStockList = repository.InsertCheckStockListInfo(checkStockList);
                if (recCheckStockList > 0)
                {
                    List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                    rfInfo.type = "PASSOBV";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                    return rfInfo;
                }
                else
                {
                    List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                    rfInfo.type = "FAIL";
                    rfInfo.message = "操作失败!请检查";
                    rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
                    return rfInfo;
                }
            }

            if (wmsCheckStocks.Count > 0)
            {
                if (wmsCheckStocks[0].OrderStatus == "20")//修改为盘点中状态
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                if (wmsCheckStocks[0].LocateName.ToUpper() != locateName.ToUpper())//库位不一致
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='60'," +
                        " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                        "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                        "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                        ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "' ;");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) == Convert.ToInt32(qty))//一致
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='20'," +
                                  " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                  "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                  "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                  ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) < Convert.ToInt32(qty))//盘盈
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='30'," +
                             " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                             "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                             "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                             ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
                else if (Convert.ToInt32(wmsCheckStocks[0].Qty) > Convert.ToInt32(qty))//盘亏
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE wms_check_stock_list SET fact_qty=" + qty + ",trans_status='40'," +
                                 " dest_warehouse_id=" + sysLocates.WarehouseId + ",dest_warehouse_name='" + sysLocates.WarehouseName + "'," +
                                 "dest_zone_id=" + sysLocates.ZoneId + ",dest_zone_name='" + sysLocates.ZoneName + "'," +
                                 "dest_locate_id=" + sysLocates.LocateId + ",dest_locate_name='" + sysLocates.LocateName + "' " +
                                 ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "' and order_no='" + orderNo + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
            }

            int refNum = repository.SQLTransaction(sqlStrings, parameterList);
            if (refNum > 0)
            {
                List<WmsCheckStock> wmsChecks1 = repository.GetWmsCheckStockQtyFactByLocateName(orderNo, locateName);
                rfInfo.type = "PASS";
                rfInfo.message = "操作成功!请继续扫描箱条码";
                rfInfo.wmsObjectDetail = wmsChecks1.ConvertAll(s => (object)s);
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;

        }
        */

    }
}
